<template>
    <div class="main">
        <go-bread :beacklist="beacklist"></go-bread>
        <div class="box">
            <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" effect="dark"></el-alert>
            <el-form style="margin-top:20px;">
                <el-form-item label="选阵商品分类" label-width="100px" prop="pics">
                    <el-cascader expand-trigger="hover" v-model="pics" :options="options" :props="cateProp"
                        @change="handleChange"></el-cascader>
                </el-form-item>
            </el-form>
            <el-tabs type="border-card" v-model="activeName" @tab-click="handleTabClick">
                <el-tab-pane label="动态参数" name="many">
                    <el-button @click="dtAdd" type="primary" size="mini" :disabled="disables">添加参数</el-button>
                    <el-table :data="manyTabData" style="width: 100%">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="left" inline class="demo-table-expand">
                                    <el-form-item label>
                                        <el-tag v-for="(item, index) in scope.row.attr_vals" :key="index" closable
                                            :disable-transitions="false" @close="handleClose(index, scope.row)">{{  item 
                                            }}</el-tag>
                                        <el-input class="input-new-tag" v-if="scope.row.inputVisible"
                                            v-model="scope.row.inputValue" ref="saveTagInput" size="small"
                                            @keyup.enter.native="handleInputConfirm(scope.row)"
                                            @blur="handleInputConfirm(scope.row)"></el-input>
                                        <el-button v-else class="button-new-tag" size="small"
                                            @click="showInput(scope.row)">
                                            + New Tag</el-button>
                                    </el-form-item>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="#" type="index"></el-table-column>
                        <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button @click="showEditData(scope.row)" type="primary" icon="el-icon-edit"
                                    size="mini">
                                    修改</el-button>
                                <el-button @click="removeData(scope.row.attr_id)" type="danger" icon="el-icon-delete"
                                    size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="静态属性" name="only">
                    <el-button @click="jtAdd" type="primary" size="mini" :disabled="disables">添加属性</el-button>
                    <el-table :data="onlyTabData" style="width: 100%">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="left" inline class="demo-table-expand">
                                    <el-form-item label>
                                        <el-tag v-for="(item, index) in scope.row.attr_vals" :key="index" closable
                                            :disable-transitions="false" @close="handleClose(index, scope.row)">{{  item 
                                            }} </el-tag>
                                        <el-input class="input-new-tag" v-if="scope.row.inputVisible"
                                            v-model="scope.row.inputValue" ref="saveTagInput" size="small"
                                            @keyup.enter.native="handleInputConfirm(scope.row)"
                                            @blur="handleInputConfirm(scope.row)"></el-input>
                                        <el-button v-else class="button-new-tag" size="small"
                                            @click="showInput(scope.row)"> + New Tag</el-button>
                                    </el-form-item>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="#" type="index"></el-table-column>
                        <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button @click="showEditData(scope.row)" type="primary" icon="el-icon-edit"
                                    size="mini">
                                    修改</el-button>
                                <el-button @click="removeData(scope.row.attr_id)" type="danger" icon="el-icon-delete"
                                    size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                </el-tab-pane>
            </el-tabs>
            <!-- 动态参数或静态属性 -->
            <el-dialog :title="this.activeName == 'only' ? '修改静态属性' : '修改动态参数'" :visible.sync="editDialogVisible"
                width="50%">
                <el-form :model="editDataForm" :rules="editRules" ref="editDataForm" label-width="100px">
                    <el-form-item :label="this.activeName == 'only' ? '静态属性' : '动态参数'" prop="attr_name">
                        <el-input v-model="editDataForm.attr_name"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editDataTrue">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 添加动态参数或静态属性 -->
            <el-dialog :title="this.activeName == 'only' ? '添加静态属性' : '添加动态参数'" :visible.sync="addDialogVisible"
                width="50%" :before-close="handleCloses" @close="addDialogClosed">
                <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                    <el-form-item :label="this.activeName == 'only' ? '静态属性' : '动态参数'" prop="attr_name">
                        <el-input v-model="addForm.attr_name"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addAttrTrue">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>
  
  <script>
import {
    cascaderList,//获取商品分类内容
    goodsCan,//获取表格数据
    delParamsPort,
    addAttr,//添加动态参数或静态属性
    editData,//修改动态参数或静态属性
    addChild
} from "@/api/goods";
import { mapGetters } from "vuex";
export default {
    data() {
        return {
            activeName: "many",
            pics: [],
            options: [],
            cateProp: {//联机下拉框展示数据
                label: "cat_name",
                value: "cat_id",
                children: "children"
            },
            manyTabData: [],//动态参数列表数据
            onlyTabData: [],//静态属性列表数据
            editDataForm: [],//修改数据
            editDialogVisible: false,
            editRules: {
                attr_name: [{ required: true, message: "请输入参数", tirgger: "blur" }]
            },
            addDialogVisible: false,
            addForm: { //添加数据
                attr_name: ""
            },
            rules: {
                attr_name: [{ required: true, message: "请输入参数", tirgger: "blur" }]
            },
            
        };
    },
    computed: {
        disables() {
            return this.pics.length == 3 ? false : true
        },
        // 获取子级的id
        cateId() {
            return this.pics.length == 3 ? this.pics[this.pics.length - 1] : null
        },
        // 计算dialog标题
        titleText() {
            if (this.activeName == "many") {
                return "动态参数";
            } else {
                return "静态属性";
            }
        },
        ...mapGetters(['beacklist'])
    },
    created() { },
    mounted() {
        this.getCascader();
    },
    watch: {},
    methods: {

        async handleChange(a) { // 级联选择器
            if (this.pics.length !== 3) {
                this.pics = [];
                return;
            }
            // 获取动态参数或静态属性
            // console.log(this.pics);
            // console.log(this.disables);
            if (!this.disables) {
                this.getData(this.cateId, this.activeName);
            }
        },

        async handleTabClick() { // 点击标签页触发事件
            // console.log(!this.disables);
            // console.log(this.pics);
            if (!this.disables) {
                this.getData(this.cateId, this.activeName);
            }else{
                this.manyTabData = []
                this.onlyTabData = []
            }
            

        },

        async getCascader() { // 获取商品分类内容
            let res = await cascaderList();
            // console.log(res);
            if (res.meta.status == 200) {
                this.options = res.data;
            }
        },

        async getData(id, name) { // 获取表格数据
            let res = await goodsCan(id, name);
            if (res.meta.status == 200) {
                res.data.forEach(item => {
                    item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
                    // 控制文本框的显示与隐藏
                    item.inputVisible = false;
                    item.inputValue = "";
                });
                // console.log(res.data);
                if (name == "many") {
                    this.manyTabData = res.data;
                } else {
                    this.onlyTabData = res.data;
                }
            }
        },

        dtAdd() { // 添加动态参数
            this.addDialogVisible = true;
        },

        jtAdd() { // 添加静态属性
            this.addDialogVisible = true;
        },

        addDialogClosed() { // 关闭dialog清空表单
            this.$refs["addForm"].resetFields();
        },

        removeData(id) { // 删除动态参数或静态属性
            this.$confirm("此操作将永久删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(async () => {
                    let res = await delParamsPort(this.cateId, id)
                    console.log(res);
                    console.log(this.cateId, id);
                    if (res.meta.status == 200) {
                        this.$message.success(res.meta.msg);
                        this.getData(this.cateId, this.activeName);
                    }
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        },

        showEditData(data) { // 修改动态参数或静态属性
            this.editDataForm = data;
            this.editDialogVisible = true;
        },

        async editDataTrue() { // 修改动态参数或静态属性
            let res = await editData(
                this.cateId,
                this.editDataForm.attr_id,
                this.editDataForm.attr_name,
                this.activeName
            );
            console.log(res);
            if ((res.meta.status = 200)) {
                this.$message.success(res.meta.msg);
                this.getData(this.cateId, this.activeName);
            }
            this.editDialogVisible = false;
        },

        async handleClose(i, row) { // 删除动态参数子级
            row.attr_vals.splice(i, 1)
            this.getChildren(row)
        },

        async addAttrTrue() { // 添加动态参数或静态属性
            let res = await addAttr(
                this.cateId,
                this.addForm.attr_name,
                this.activeName
            );
            console.log(res);
            if (res.meta.status == 201) {
                this.$message.success(res.meta.msg);
                // 获取动态参数或静态属性
                this.getData(this.cateId, this.activeName);
            }
            this.addDialogVisible = false;
        },

        handleCloses() {

        },

        showInput(row) { // 点击改变输入框
            row.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },

        async handleInputConfirm(row) {
            if (row.inputValue.trim().length == 0) {
                row.inputValue = "";
                row.inputVisible = false;
                return;
            }
            let inputValue = row.inputValue.trim();
            if (inputValue) {
                // 添加子级
                row.attr_vals.push(inputValue);
                this.getChildren(row)
            }

            row.inputVisible = false;
            row.inputValue = "";
        },

        async getChildren(row) { // 动态参数静态属性子级
            let res = await addChild(
                this.cateId,
                row.attr_id,
                {
                    attr_name: row.attr_name,
                    attr_sel: row.attr_sel,
                    attr_vals: row.attr_vals.join(",")
                }
            );
            console.log(res);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                // this.getData(this.cateId, this.activeName);
            }
        },
    },
    components: {}
};
</script>
  
  <style>
  .box {
      /* margin: 20px 0 0 0; */
      padding: 20px 20px 0 20px;
      background: #fff;
      min-height: 500px;
  }
  
  .el-tag+.el-tag {
      margin-left: 10px;
  }
  
  .button-new-tag {
      margin-left: 10px;
      height: 32px;
      line-height: 30px;
      padding-top: 0;
      padding-bottom: 0;
  }
  
  .input-new-tag {
      width: 90px;
      margin-left: 10px;
      vertical-align: bottom;
  }
  
  .el-cascader-panel {
      height: 300px;
  }
  </style>
  